package com.tensquare.article.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.feign.NoticeFeignClient;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.pojo.Notice;
import com.tensquare.article.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author BAO
 * @version V1.0
 * @Package com.tensquare.article.service.impl
 * @date 2020/12/2 20:56
 * @description:
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private NoticeFeignClient noticeFeignClient;

    @Override
    public Page<Article> findAll() {
        log.info("success---findAll");
        Page<Article> page = new Page<>(0, 5);
        List<Article> list = articleDao.selectPage(page, null);
        page.setRecords(list);
        return page;
    }

    @Override
    public Article findById(String id) {
        log.info("success---findById");
        return articleDao.selectById(id);
    }

    @Override
    public int add(Article article) {
        log.info("success---add");
        return articleDao.insert(article);
    }

    @Override
    public int update(Article article) {
        log.info("success---update");
        return articleDao.updateById(article);
    }

    @Override
    public int deleteById(String id) {
        log.info("success---deleteById");
        return articleDao.deleteById(id);
    }

    @Override
    public Page<Article> pageSearch(Map map, Integer currentPage, Integer size) {
        log.info("success---pageSearch");
        EntityWrapper<Article> wrapper = new EntityWrapper<>();
        Set<String> fields = map.keySet();
        fields.forEach(field-> wrapper.eq(null!=field,field,map.get(field)));
        Page<Article> page = new Page<>(currentPage, size);
        List<Article> list = articleDao.selectPage(page, wrapper);
        page.setRecords(list);
        return page;
    }

    /**
     * 订阅或者取消订阅文章作者
     * @param userId
     * @param articleId
     * @return
     */
    @Override
    public Boolean subscribe(String userId, String articleId) {
        //根据文章id查询文章
        Article article = articleDao.selectById(articleId);

        String userKey = "article_subscribe_" + userId;
        String authorKey = "article_author_" + article.getUserid();

        //查询该用户是否已经订阅作者
        Boolean flag = redisTemplate.boundSetOps(userKey).isMember(article.getUserid());
        if (flag){
            //如果为flag为true，已经订阅,则取消订阅
            redisTemplate.boundSetOps(userKey).remove(article.getUserid());
            redisTemplate.boundSetOps(authorKey).remove(userId);
            return false;
        }
        // 如果为flag为false，没有订阅，则进行订阅
        redisTemplate.boundSetOps(userKey).add(article.getUserid());
        redisTemplate.boundSetOps(authorKey).add(userId);
        return true;
    }

    /**
     * 文章点赞,点赞数加一
     * @param articleId
     */
    @Override
    public void thumbup(String articleId) {
        Article article = articleDao.selectById(articleId);
        article.setThumbup(article.getThumbup()+1);
        articleDao.updateById(article);
    }

    /**
     * 点赞通知
     * @param articleId
     * @param userid
     */
    @Override
    public void thumbupNotify(String articleId, String userid) {
        //文章点赞
        Article article = articleDao.selectById(articleId);
        article.setThumbup(article.getThumbup()+1);
        articleDao.updateById(article);
        //消息通知
        Notice notice = new Notice();
        notice.setReceiverId(article.getUserid());
        notice.setOperatorId(userid);
        notice.setAction("thumbup");
        notice.setTargetType("article");
        notice.setTargetId(articleId);
        notice.setCreatetime(new Date());
        notice.setType("user");
        notice.setState("0");
        noticeFeignClient.add(notice);
    }
}
